package com.lzleang.dom4jReader;

import com.lzleang.model.Student;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class DOM4jReader {
   public static void testDom4jParser() throws DocumentException {
       Document document = new SAXReader().read(new File("src/student.xml"));
       Element root = document.getRootElement();
       List elements = root.elements();
       ArrayList<Student> students = new ArrayList<>();
       for (int i = 0 ; i < elements.size() ; i++){
           Student student = new Student();
           Element element = (Element)elements.get(i);
            //获取该节点所有的属性值
           List attributes = element.attributes();
           for(int j = 0 ; j < attributes.size() ; j ++){
               Attribute attribute = (Attribute)attributes.get(j);
               if("id".equals(attribute.getName())){
                   student.setId(Integer.parseInt(attribute.getValue()));
               }
           }
           //获取该节点的所有子节点
           List childElements = element.elements();
           for(int j = 0 ; j < childElements.size() ; j++){
               Element element1 = (Element)childElements.get(j);
               if("name".equals(element1.getName())){
                   student.setName(element1.getText());
               }
               if("sex".equals(element1.getName())){
                   student.setSex(element1.getText());
               }
               if("age".equals(element1.getName())){
                   student.setAge(Integer.parseInt(element1.getText()));
               }
           }
           students.add(student);
       }
       System.out.println(students);
   }

   //属性操作相关
    public static void attributeHandler() throws DocumentException, IOException {
        Document document = new SAXReader().read(new File("src/student.xml"));
        Element root = document.getRootElement();

        //新增标签属性
        Element student = (Element) root.elements("student").get(0);
        student.addAttribute("class","三年二班");

        //修改标签属性
        student.attribute("id").setValue("99");

        //删除标签属性
        Element student1 = (Element) root.elements("student").get(1);
        Attribute attribute = student1.attribute("id");
        student1.remove(attribute);

        saveXML(document);
    }


    //标签操作相关
    public static void updateDomHandler() throws DocumentException, IOException {
        Document document = new SAXReader().read(new File("src/student.xml"));
        Element root = document.getRootElement();


        //新增标签
        Element student = root.addElement("student");
        student.addAttribute("id","3");
        student.addElement("name").setText("钱九");
        student.addElement("sex").setText("男");
        student.addElement("age").setText("23");


        //修改标签
        Element element = (Element)root.elements().get(0);
        List elements = element.elements();
        for(int i = 0 ; i < elements.size() ; i++){
            Element element1 = (Element)elements.get(i);
            if("name".equals(element1.getName())){
                element1.setText("王五");
            }
        }

        //删除标签
        Element element1 = (Element)root.elements().get(1);
        element1.getParent().remove(element1);

        saveXML(document);
    }

    //将数据写入xml文件
    public static void saveXML(Document document) throws IOException {
        OutputFormat outformat = new OutputFormat();
        // 指定XML编码
        outformat.setEncoding("UTF-8");
        outformat.setNewlines(true);
        outformat.setIndent(true);
        outformat.setTrimText(true);

        String rootPath = System.getProperty("user.dir");

        OutputStream out = new FileOutputStream("src/student.xml");

        XMLWriter xmlwriter = new XMLWriter(out, outformat);

        xmlwriter.write(document);
    }
    public static void main(String[] args) throws DocumentException, IOException {
        updateDomHandler();
    }
}
